package org.springblade.modules.admin.utils;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springblade.core.tool.api.ResultCode;
import org.springblade.core.tool.utils.Func;

import java.io.Serializable;

/**
 * @Description 梯联网统一API响应结果封装
 * @Author ysq
 * @Date 2022/12/3
 **/
@Getter
@Setter
@ToString
@ApiModel(description = "梯联网返回信息")
@NoArgsConstructor
public class TlwR implements Serializable {

	private static final long serialVersionUID = 1L;

	private static JSONObject nullR;

	static {
		JSONConfig config = new JSONConfig();
		config.setIgnoreNullValue(false);
		nullR = new JSONObject(config);
		nullR.set("code", 200);
		nullR.set("success", true);
		nullR.set("msg", "暂无承载数据");
		nullR.set("data", null);
	}

	public static JSONObject data(JSONObject data) {
		if (data == null || data.size() == 0) {
			return nullR;
		}
		int code = data.getInt("code");
		String msg = data.getStr("msg");
		if (code != ResultCode.SUCCESS.getCode()) {
			data.set("msg", String.format("梯联网数据返回异常: {%s}", msg));
		}
		return data;
	}

	public static JSONObject data(String data) {
		JSONObject jsonData = JSONUtil.parseObj(data);
		return data(jsonData);
	}

}
